package com.csglxt.view.action;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csglxt.base.BaseAction;
import com.csglxt.domain.GoodsCate;

/**
 * 
 * @ClassName: GoodsCateAction
 * @Description: TODO(商品分类管理控制器 商品分类的查询，添加、修改、删除)
 * @author Itchbeilef
 * @date 2015-5-28 下午8:44:51
 * 
 */
@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class GoodsCateAction extends BaseAction<GoodsCate> {
    /**
     * 
     * @Title: addCateUI
     * @Description: TODO(加载添加商品分类页面)
     * @param
     * @return
     * @throws
     */
    public String addCateUI() {
        return "saveCateUI";
    }

    /**
     * 
     * @Title: editCateUI
     * @Description: TODO(加载修改商品分类页面)
     * @param
     * @return
     * @throws
     */
    public String editCateUI() {
        getValueStack().push(goodsCateService.getById(model.getId()));
        return "saveCateUI";
    }

    /**
     * 
     * @Title: addCate
     * @Description: TODO(添加商品分类操作到数据库)
     * @param
     * @return
     * @throws
     */
    public String addCate() {
        if (model != null) {
            goodsCateService.save(model);
        }
        return "redirectCatelist";
    }

    /**
     * 
     * @Title: upCate
     * @Description: TODO(修改商品分类到数据库)
     * @param
     * @return
     * @throws
     */
    public String upCate() {
        if (model != null) {
            goodsCateService.update(model);
        }

        return "redirectCatelist";
    }

    /**
     * 
     * @Title: delGoodsCate
     * @Description: TODO(删除商品分类到数据库)
     * @param
     * @return
     * @throws
     */
    public String delGoodsCate() {
        if (model.getId() != null) {
            goodsCateService.delete(model.getId());
        }
        return "redirectCatelist";
    }

    // ajax验证
    public void ajx() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();

        response.setContentType("text/html;charset=utf-8");
        // 处理get请求乱码
        model.setName(new String(model.getName().getBytes("ISO-8859-1"),
                "UTF-8"));
        GoodsCate goodscate = goodsCateService.findByname(model);

        if (goodscate != null) {
            // 重点用是用print输出的数据格式是json

            response.getWriter().print("{\"success\":true,\"msg\":\"已存在\"}");
        } else {

            response.getWriter().print("{\"success\":false,\"msg\":\"\"}");
        }
    }
}
